package indi.kider.server.invoice.api;

import indi.kider.common.constant.EmployeeRoleCons;
import indi.kider.common.model.employee.Employee;
import indi.kider.common.model.invoice.Invoice;
import indi.kider.common.model.invoice.InvoiceLogQuery;
import indi.kider.common.model.invoice.InvoiceLogs;
import indi.kider.server.invoice.related.AuthcService;
import indi.kider.server.invoice.service.InvoiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.vankid.common.model.base.CommonResponse;
import pers.vankid.common.model.base.CoreException;
import pers.vankid.common.utils.page.PageRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("invoice")
public class InvoiceController {
    @Autowired
    private InvoiceService invoiceService;
    @Autowired
    private AuthcService authcService;
    /**
     * @api {POST} /
     * @apiGroup invoice
     * @apiDescription 新增资产 物品类型（1、家具家电2、电子设备3、钥匙4、安防器材）
     * @apiParamExample 请求样例：
     * http://localhost:9002/invoice-manage/invoice
     * {"itemName":"空调","numbs":"0","price":"sss","brandName":"sss","itemType":"1"}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"201","msg":"保存成功！","data":{"itemName":"空调","numbs":"0","price":"sss","brandName":"sss","itemType":"1"}}
     */
    @PostMapping("")
    public CommonResponse add(@RequestBody Invoice invoice){
        try{
            Employee employee = authcService.getCurrentUser().getData();
            invoice.setEnterpriseId(employee.getEnterpriseId());
            return CommonResponse.buildAddSuccessResponse(invoiceService.add(invoice)) ;
        }catch (CoreException e){
            return CommonResponse.buildCoreExceptionResponse(e);
        }
    }
    /**
     * @api {PUT} /
     * @apiGroup invoice
     * @apiDescription 资产买入、拆回  流入方向（1买入，2装出，3拆回，4报废）
     * @apiParamExample 请求样例：
     * http://localhost:9002/invoice-manage/invoice
     * [{"invoiceId":"1","numbs":"1","direction":"1","employeeId":"1","remark":"dasd","address":"dasd","workOrder":"20180103001"}]
     * @apiSuccessExample {json} 返回样例:
     * {"status":"202","msg":"保存成功！","data":[{"id":1,"itemName":"空调","numbs":"1","price":"sss","brandName":"sss","itemType":"1"}]}
     */
    @PutMapping("")
    public CommonResponse manage(@RequestBody List<InvoiceLogs> logs){
        try{
            return CommonResponse.buildUpdateSuccessResponse(invoiceService.manage(logs)) ;
        }catch (CoreException e){
            return CommonResponse.buildCoreExceptionResponse(e);
        }
    }
    /**
     * @api {POST} /plus
     * @apiGroup invoice
     * @apiDescription 资产买入、拆回  流入方向（1买入，2装出，3拆回，4报废）
     * @apiParamExample 请求样例：
     * http://localhost:9002/invoice-manage/invoice/plus
     * [{"invoiceId":"1","numbs":"1","direction":"1","employeeId":"1","remark":"dasd","address":"dasd","workOrder":"20180103001"}]
     * @apiSuccessExample {json} 返回样例:
     * {"status":"202","msg":"保存成功！","data":[{"id":1,"itemName":"空调","numbs":"1","price":"sss","brandName":"sss","itemType":"1"}]}
     */
    @PostMapping("plus")
    public CommonResponse plus(@RequestBody List<InvoiceLogs> logs){
        try {
            return CommonResponse.buildUpdateSuccessResponse(invoiceService.plus(logs)) ;
        }catch (CoreException e){
            return CommonResponse.buildCoreExceptionResponse(e);
        }
    }
    /**
     * @api {POST} /minus
     * @apiGroup invoice
     * @apiDescription 资产装出、报废  流入方向（1买入，2装出，3拆回，4报废）
     * @apiParamExample 请求样例：
     * http://localhost:9002/invoice-manage/invoice/minus
     * [{"invoiceId":"1","numbs":"1","direction":"2","employeeId":"1","remark":"dasd","address":"dasd","workOrder":"20180103002"}]
     * @apiSuccessExample {json} 返回样例:
     * {"status":"202","msg":"保存成功！","data":[{"id":1,"itemName":"空调","numbs":"0","price":"sss","brandName":"sss","itemType":"1"}]}
     */
    @PostMapping("minus")
    public CommonResponse minus(@RequestBody List<InvoiceLogs> logs){
        try {
            return CommonResponse.buildUpdateSuccessResponse(invoiceService.minus(logs)) ;
        }catch (CoreException e){
            return CommonResponse.buildCoreExceptionResponse(e);
        }

    }
    /**
     * @api {POST} /all
     * @apiGroup invoice
     * @apiDescription 查询资产（不分页） 物品类型（1、家具家电2、电子设备3、钥匙4、安防器材）
     * @apiParamExample 请求样例：
     * http://localhost:9002/invoice-manage/invoice/all
     * {"itemName":"空调","numbs":"0","price":"sss","brandName":"sss","itemType":"1"}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":[{"id":1,"itemName":"空调","numbs":"0","price":"sss","brandName":"sss","itemType":"1"}]}
     */
    @PostMapping("all")
    public CommonResponse query(@RequestBody Invoice invoice){
        try{
            Employee employee = authcService.getCurrentUser().getData();
            if(!employee.getRoles().equals(EmployeeRoleCons.ADMIN)){//非超级管理员
                invoice.setEnterpriseId(employee.getEnterpriseId());
            }
            return CommonResponse.buildGetSuccessResponse(invoiceService.query(invoice)) ;
        }catch (CoreException e){
            return CommonResponse.buildCoreExceptionResponse(e);
        }
    }
    /**
     * @api {POST} /list
     * @apiGroup invoice
     * @apiDescription 查询资产（分页） 物品类型（1、家具家电2、电子设备3、钥匙4、安防器材）
     * @apiParamExample 请求样例：
     * http://localhost:9002/invoice-manage/invoice/list
     * {"num":"0","size":"8","query":{"itemName":"空调","numbs":"0","price":"sss","brandName":"sss","itemType":"1"}}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"rows":[{"id":1,"itemName":"空调","numbs":"0","price":"sss","brandName":"sss","itemType":"1"}],"total":1,"size":0}}
     */
    @PostMapping("list")
    public CommonResponse query(@RequestBody PageRequest<Invoice> request){
        try{
            Employee employee = authcService.getCurrentUser().getData();
            if(!employee.getRoles().equals(EmployeeRoleCons.ADMIN)){//非超级管理员
                request.getQuery().setEnterpriseId(employee.getEnterpriseId());
            }
            return CommonResponse.buildGetSuccessResponse(invoiceService.query(request)) ;
        }catch (CoreException e){
            return CommonResponse.buildCoreExceptionResponse(e);
        }
    }
    /**
     * @api {POST} /log/list
     * @apiGroup invoice
     * @apiDescription 查询资产操作记录（分页） 物品类型（1、家具家电2、电子设备3、钥匙4、安防器材）
     * @apiParamExample 请求样例：
     * http://localhost:9002/invoice-manage/invoice/log/list
     * {"num":"0","size":"8","query":{"itemName":"空调","numbs":"0","price":"sss","brandName":"sss","itemType":"1","logsNumbs":"1","direction":"2","employeeId":"1","remark":"dasd","address":"dasd","workOrder":"20180103002"}}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"rows":[{"logId":1,"invoiceId":1,"itemName":"空调","numbs":"0","price":"sss","brandName":"sss","itemType":"1","logsNumbs":"1","direction":"2","employeeId":"1","remark":"dasd","address":"dasd","workOrder":"20180103002"}],"total":1,"size":0}}
     */
    @PostMapping("log/list")
    public CommonResponse queryLogs(@RequestBody PageRequest<InvoiceLogQuery> request){
        try{
            Employee employee = authcService.getCurrentUser().getData();
            if(!employee.getRoles().equals(EmployeeRoleCons.ADMIN)){//非超级管理员
                request.getQuery().setEnterpriseId(employee.getEnterpriseId());
            }
            return CommonResponse.buildGetSuccessResponse(invoiceService.queryLogs(request)) ;
        }catch (CoreException e){
            return CommonResponse.buildCoreExceptionResponse(e);
        }
    }
}
